package com.henz4help.app.db;

import java.util.GregorianCalendar;

import com.henz4help.app.common.Util;
import com.henz4help.app.db.domain.PersonalData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class HenzDBHelper {
	private static HenzDBHelper mHenzDBHelper;
	private static final String DATABASE_NAME = "henz.db";
	private static final int DATABASE_VERSION = 9;
	private SQLiteDatabase mDB;
	private DatabaseHelper mDBHelper;
	private Context mCtx;
	
	public static HenzDBHelper getInstance(Context ctx) {
		if(mHenzDBHelper == null) {
			mHenzDBHelper = new HenzDBHelper(ctx);
			mHenzDBHelper.open();
		}
		
		return mHenzDBHelper;
	}
	
	public static void destroyHenzDBHelper() {
		if(mHenzDBHelper != null) {
			mHenzDBHelper.close();
			mHenzDBHelper = null;
		}
	}
	
	private class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(HenzDB.TPersonalData.CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + HenzDB.TPersonalData.TABLE_NAME);
			
			onCreate(db);
		}
	}
	
	public HenzDBHelper(Context context) {
		this.mCtx = context;
	}
	
	public HenzDBHelper open() throws SQLException {
		mDBHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);                
		mDB = mDBHelper.getWritableDatabase();
		
		return this;
	}
	
	public void close() {
		mDB.close();
	}
	
	/** Personal Data **/
	public PersonalData retrievePersonalData() {
		Cursor cursor = mDB.query(HenzDB.TPersonalData.TABLE_NAME, null, null, null, null, null, null);
		
		Util.Log("retrievePersonalData[" + cursor + "][" + cursor.getCount() + "]");
		
		if(cursor == null || cursor.getCount() == 0)
			return null;
		
		PersonalData data = new PersonalData();
		
		cursor.moveToFirst();
		
		data.additionalMessage  = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.ADDITIONAL_MESSAGE)   );
		data.address            = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.ADDRESS)              );
		data.apartment          = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.APARTMENT)            );
		
		data.latitude           = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.LATITUDE)             );
		data.longitude          = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.LONGITUDE)            );
		
		data.birthDay           = cursor.getInt   ( cursor.getColumnIndex(HenzDB.TPersonalData.BIRTH_DAY)            );
		data.birthMonth         = cursor.getInt   ( cursor.getColumnIndex(HenzDB.TPersonalData.BIRTH_MONTH)		     );
		data.birthYear          = cursor.getInt   ( cursor.getColumnIndex(HenzDB.TPersonalData.BIRTH_YEAR)           );
		data.city               = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.CITY)                 );
		
		data.email              = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.EMAIL)                );
		
		data.firstName          = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.FIRST_NAME)           );
		data.gender             = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.GENDER)               );
		data.insurance          = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.INSURANCE)		     );
		data.insuranceAccountNo = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.INSURANCE_ACCOUNT_NO) );
		data.lastName           = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.LAST_NAME)            );
		data.middleInitial      = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.MIDDLE_INITIAL)       );
		data.organDonor         = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.ORGAN_DONOR)          );
		data.street             = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.STREET)               );
		data.zip                = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.ZIP)                  );
		data.state              = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.STATE)                );
		data.uriString          = cursor.getString( cursor.getColumnIndex(HenzDB.TPersonalData.URI_STRING)           );
		data.createDate         = cursor.getLong  ( cursor.getColumnIndex(HenzDB.TPersonalData.CREATE_DATE)          );
		data.updateData         = cursor.getLong  ( cursor.getColumnIndex(HenzDB.TPersonalData.UPDATE_DATE)          );
		
		if(cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		
		return data;
	}
	
	protected ContentValues getCont(PersonalData data){
		ContentValues returnValue = new ContentValues();
		GregorianCalendar calendar = new GregorianCalendar();
		long milliTime = calendar.getTimeInMillis();
		
		returnValue.put(HenzDB.TPersonalData.ADDITIONAL_MESSAGE, data.additionalMessage);
		returnValue.put(HenzDB.TPersonalData.ADDRESS, data.address);
		returnValue.put(HenzDB.TPersonalData.APARTMENT, data.apartment);
		
		returnValue.put(HenzDB.TPersonalData.LATITUDE , data.latitude);
		returnValue.put(HenzDB.TPersonalData.LONGITUDE, data.longitude);
		
		returnValue.put(HenzDB.TPersonalData.BIRTH_DAY, data.birthDay);
		returnValue.put(HenzDB.TPersonalData.BIRTH_MONTH, data.birthMonth);
		returnValue.put(HenzDB.TPersonalData.BIRTH_YEAR, data.birthYear);
		returnValue.put(HenzDB.TPersonalData.CITY, data.city);
		
		//Util.Log("TPersonalData.EMAIL[" + HenzDB.TPersonalData.EMAIL + "][" + data.email + "]");
		returnValue.put(HenzDB.TPersonalData.EMAIL, data.email);
		
		returnValue.put(HenzDB.TPersonalData.FIRST_NAME, data.firstName);
		returnValue.put(HenzDB.TPersonalData.GENDER, data.gender);
		returnValue.put(HenzDB.TPersonalData.INSURANCE, data.insurance);
		returnValue.put(HenzDB.TPersonalData.INSURANCE_ACCOUNT_NO, data.insuranceAccountNo);
		returnValue.put(HenzDB.TPersonalData.LAST_NAME, data.lastName);
		returnValue.put(HenzDB.TPersonalData.MIDDLE_INITIAL, data.middleInitial);
		returnValue.put(HenzDB.TPersonalData.ORGAN_DONOR, data.organDonor);
		returnValue.put(HenzDB.TPersonalData.STREET, data.street);
		returnValue.put(HenzDB.TPersonalData.ZIP, data.zip);
		returnValue.put(HenzDB.TPersonalData.STATE, data.state);
		returnValue.put(HenzDB.TPersonalData.URI_STRING, data.uriString);
		returnValue.put(HenzDB.TPersonalData.UPDATE_DATE, milliTime);
		
		return returnValue;
	}
	
	public long insertPersonalData(PersonalData data) {
		ContentValues pInfo = null;
		pInfo = getCont(data);
		pInfo.put(HenzDB.TPersonalData.CREATE_DATE, pInfo.getAsLong(HenzDB.TPersonalData.UPDATE_DATE));
		return mDB.insert(HenzDB.TPersonalData.TABLE_NAME, null, pInfo);
		
	}
	
	public long updatePersonalData(PersonalData data) {
		ContentValues pInfo = null;
		pInfo = getCont(data);
		return mDB.update(HenzDB.TPersonalData.TABLE_NAME, pInfo, null, null);
	}
}
